import numpy as np
import csv


def load_data(file_path):
    data = []
    with open(file_path, 'r', encoding='utf-8') as file:
        reader = csv.reader(file)
        next(reader)
        for row in reader:
            stars = int(row[2])
            forks = int(row[3])
            created_at = np.datetime64(row[5])
            last_commit = np.datetime64(row[6])
            active_days = (last_commit - created_at).astype(float)
            data.append([stars, forks, active_days])
    return np.array(data)


def calculate_statistics(data):
    means = np.round(np.mean(data, axis=0), 1)
    medians = np.round(np.median(data, axis=0), 1)
    variances = np.round(np.var(data, axis=0), 1)
    stds = np.round(np.std(data, axis=0), 1)
    return {
       'means': means,
       'medians': medians,
       'variances': variances,
       'stds': stds
    }


def print_results(stats):
    metrics = ['Stars', 'Forks', 'Active Days']
    for metric, mean, med, var, std in zip(metrics,
                                           stats['means'],
                                           stats['medians'],
                                           stats['variances'],
                                           stats['stds']):
        print(f"{metric}:")
        print(f"    Average: {mean:.1f}")
        print(f"    Median: {med:.1f}")
        print(f"    Variance: {var:.1f}")
        print(f"    Standard Deviation: {std:.1f}")


# 假设这里使用 pakistan-repos.csv 文件，你也可以改成 china-repos.csv
repo_data = load_data('pakistan-repos.csv')
stats = calculate_statistics(repo_data)
print_results(stats)
